/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testchat;

import java.io.IOException;
import java.net.*;

/**
 *
 * @author ThanhKhanh
 */
public class ClientNhanMulticast2 {

    private static int DEST_PORT = 1234;
    private static int BUFFER_LENGTH = 1024;

    public static void main(String arg[]) throws IOException {
        byte[] b = new byte[BUFFER_LENGTH];
        try {
            DatagramPacket dgram = new DatagramPacket(b, b.length);
            MulticastSocket socket =
                    new MulticastSocket(DEST_PORT); // must bind receive side
            byte[] b2 = new byte[4];
            b2[0]=(byte) 224;
            b2[1]=0;
            b2[2]=0;
            b2[3]=1;
            Inet4Address ip = (Inet4Address) Inet4Address.getByAddress(b2);
           socket.joinGroup(ip);

            while (true) {
                socket.receive(dgram); // blocks until a datagram is received
                System.err.println("Received " + dgram.getLength()
                        + " bytes from " + dgram.getAddress() );

                dgram.setLength(b.length); // must reset length field!
            }
        } catch (Exception e) {
            System.out.print(e.getMessage());
        }
    }
}


